package com.fz.us.admin.company.service.impl;

import com.fz.us.admin.base.bean.BaseEnumManager;
import com.fz.us.admin.base.bean.Result;
import com.fz.us.admin.base.bean.search.SearchEnum;
import com.fz.us.admin.base.bean.search.order.Order;
import com.fz.us.admin.base.bean.search.page.PagePaging;
import com.fz.us.admin.base.bean.search.result.SearchResultList;
import com.fz.us.admin.base.bean.search.result.SearchResultPaging;
import com.fz.us.admin.base.bean.search.rule.Rule;
import com.fz.us.admin.base.bean.search.rule.RuleHelper;
import com.fz.us.admin.base.dao.BaseDao;
import com.fz.us.admin.base.service.impl.BaseServiceImpl;
import com.fz.us.admin.company.dao.GroupDao;
import com.fz.us.admin.company.dao.PostDao;
import com.fz.us.admin.company.entity.Company;
import com.fz.us.admin.company.entity.Group;
import com.fz.us.admin.company.entity.Post;
import com.fz.us.admin.company.entity.User;
import com.fz.us.admin.company.service.DutyService;
import com.fz.us.admin.company.service.GroupService;
import com.fz.us.admin.company.service.PostService;
import com.fz.us.admin.company.service.PowerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Service实现类 - 岗位
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author fallenpanda
 * @version 1.0 2016-08-02 。
 * ============================================================================
 */
@Service
public class GroupServiceImpl extends BaseServiceImpl<Group,String> implements GroupService {

    @Resource
    private GroupDao groupDao;
    @Override
    public BaseDao<Group, String> getBaseDao() {
        return groupDao;
    }


    @Override
    public List<String> findGroupIdByUser(User user) {
        return groupDao.findGroupIdByUser(user);
    }

    @Override
    public Group findGroupByName(String name) {
        return groupDao.findGroupByName(name);
    }

    @Override
    public SearchResultPaging pagingGroup(PagePaging page, List<Rule> rules, Order order, Company company, BaseEnumManager.StateEnum... states) {
        Assert.notNull(company, "company is required");

        if (rules == null)
            rules = new ArrayList<Rule>();
        rules.add(RuleHelper.eq("company", company));
        if (states != null && states.length > 0) {
            rules.add(RuleHelper.in("state", states));
        }

        // 默认排序
        if (order == null)
            order = Order.order("id", SearchEnum.OrderType.asc);

        return findPagingBySearch(page, rules, order);
    }

    @Override
    public SearchResultList listGroup(List<Rule> rules, Order order, Company company, BaseEnumManager.StateEnum... states) {
        Assert.notNull(company, "company is required");

        if (rules == null)
            rules = new ArrayList<Rule>();
        rules.add(RuleHelper.eq("company", company));
        if (states != null && states.length > 0) {
            rules.add(RuleHelper.in("state", states));
        }

        // 默认排序
        if (order == null)
            order = Order.order("id", SearchEnum.OrderType.asc);

        return findListBySearch(rules, order);
    }

    @Override
    public Map<String, Object> getListMap(Group group) {

        Map<String,Object> map= new HashMap<String,Object>();
        map.put("id", group.getId());
        map.put("name", group.getName());

        map.put("pName", group.getpName());

        map.put("description", group.getDescription());
        // other
        map.put("state", group.getState()==null?"":group.getState().name());

        return map;
    }


}
